#include "WebCenterMgr.h"
#include "GM/GMManager.h"
#include "CenterService.h"
#include "MailCenter/MailManager.h"
#include "ChatCenter/ChatCenter.h"
#include "PayCenterMgr/PayCenterMgr.h"
#include "ProtoBuffer/ServerInternalProtocol.pb.h"
#include "ProtoBuffer/ServerInternalCmd.pb.h"
#include "AccountMgr/CenterAccountMgr.h"

WebCenterMgr::WebCenterMgr()
{

}

WebCenterMgr::~WebCenterMgr()
{}

bool WebCenterMgr::Init()
{
    bool result = false;
    bool retCode = false;
	IMessageDispatch *pMessageDispatch = g_GetCenterService()->GetServiceMessageDispatch();
	MMOLOG_PROCESS_ERROR(nullptr != pMessageDispatch);

	retCode = pMessageDispatch->registerServerHandler(EMODULE_ID_SEND_ZONE_MSG, this);
	MMOLOG_PROCESS_ERROR(retCode);

	retCode = pMessageDispatch->registerServerHandler(EMODULE_ID_RECV_ZONE_MSG, this);
	MMOLOG_PROCESS_ERROR(retCode);

	retCode = pMessageDispatch->registerClientHandler(EMODULE_ID_SEND_ZONE_MSG, this);
	MMOLOG_PROCESS_ERROR(retCode);

	m_Func[WEB_TO_CENTER_GM] = &WebCenterMgr::WebGM;
	m_Func[WEB_SEND_MAIL_REQ] = &WebCenterMgr::WebMail;
	m_Func[SERVER_GM_RESULT] = &WebCenterMgr::WebGMResultFromLogic;
	m_Func[CENTER_REGSITER_OPWEB_RSP] = &WebCenterMgr::OpWebRigsterRsp;
	m_Func[WEB_NOTIFY_OPINFO] = &WebCenterMgr::SynWebOpInfo;
	m_Func[WEB_SEND_RUMOR_REQ] = &WebCenterMgr::WebRumor;
	m_Func[WEB_TO_CENTER_PAY_OPEN_RSP] = &WebCenterMgr::WebOpOpenReq;
	m_Func[WEB_TO_CENTER_PAY_SORT_INDEX_REQ] = &WebCenterMgr::WebOpSortReq;
	m_Func[CLIENT_TO_CENTER_CDKEY_REQ] = &WebCenterMgr::WebCdKeyReqFromClient;
	m_Func[LOGIC_TO_WEBOP_CDKEY_RSP] = &WebCenterMgr::WebCdKeyRspFromWeb;

    result = true;
Exit0:
	return result;
}

bool WebCenterMgr::UnInit()
{
    bool result = false;
    bool retCode = false;

	IMessageDispatch *pMessageDispatch = g_GetCenterService()->GetServiceMessageDispatch();
	MMOLOG_PROCESS_ERROR(nullptr != pMessageDispatch);

	retCode = pMessageDispatch->unregisterServerHandler(EMODULE_ID_SEND_ZONE_MSG);
	MMOLOG_PROCESS_ERROR(retCode);

	retCode = pMessageDispatch->unregisterServerHandler(EMODULE_ID_RECV_ZONE_MSG);
	MMOLOG_PROCESS_ERROR(retCode);

	retCode = pMessageDispatch->unregisterClientHandler(EMODULE_ID_WELFARE);
	MMOLOG_PROCESS_ERROR(retCode);

    result = true;
Exit0:
	return result;
}

void WebCenterMgr::onServerMessage(uint32_t actionId, RECV_PACKAGE &package)
{
	mFunc::iterator iter = m_Func.find(actionId);
	mFunc::iterator iterEnd = m_Func.end();

	if (iter == iterEnd)
	{
		LogErrFmtPrint("cannt find cmd frommastegMgr, cmd:%d", actionId);
		return;
	}

	(this->*iter->second)(package);
}

void WebCenterMgr::onClientMessage(uint32_t actionId, RECV_PACKAGE &package, uint64_t Id)
{
	if (actionId == CLIENT_TO_CENTER_CDKEY_REQ)
	{
		WebCdKeyReqFromClient(package);
	}
}

bool WebCenterMgr::OpWebRigsterRsp(RECV_PACKAGE& package)
{
    bool result = false;
    bool retCode = false;
	WebOpInfo webRsp;
	NotifyWebOpInfoToGate webOpInfo;

	retCode = webRsp.ParseFromArray(package.dataAfterHead, package.sizeAfterHead);
	MMOLOG_PROCESS_ERROR(retCode);

	webOpInfo.mutable_wpinfo()->CopyFrom(webRsp);

	for (int i = 0; i < webRsp.payindexs_size(); i++)
	{
		const ChannPayIndexInfo& pi = webRsp.payindexs(i);
		g_GetPayCenterMgr()->SetPayIndexs(pi.channid(), pi.indexs());
	}

	for (int j = 0; j < webRsp.payopens_size(); j++)
	{
		const ChannPayOpenInfo& po = webRsp.payopens(j);
		g_GetPayCenterMgr()->SetPayOpen(po.channid(), po.open());
	}

	LogInfoFmtPrint("Center regster to opWeb success.....zid:%d", g_GetCenterService()->GetZoneId());

	retCode = g_GetCenterService()->SendDataToAllGate(EMODULE_ID_SERV_SYS, CENTER_NOTIFY_GATE_WEBOP_INFO, &webOpInfo);
	MMOLOG_PROCESS_ERROR(retCode);

    result = true;
Exit0:
	return result;
}

bool WebCenterMgr::WebGM(RECV_PACKAGE &package)
{
    bool result = false;
    bool retCode = false;

	LogicGM gm;
	retCode = gm.ParseFromArray(package.dataAfterHead, package.sizeAfterHead);
	MMOLOG_PROCESS_ERROR(retCode);

	retCode = g_GetGMMgr()->OnWebHandleMsg(0, gm);
	MMOLOG_PROCESS_ERROR(retCode);

    result = true;
Exit0:
	return result;
}
bool WebCenterMgr::WebGMResultFromLogic(RECV_PACKAGE &package)
{
    bool result = false;
    bool retCode = false;
	retCode = g_GetCenterService()->SendDataToWebOperate(package.msg, package.len);
	MMOLOG_PROCESS_ERROR(retCode);
    result = true;
Exit0:
	return result;
}

bool WebCenterMgr::WebMail(RECV_PACKAGE &package)
{
    bool result = false;
    bool retCode = false;
	WebSendMailReq webMail;
	ProtoBuf::GMResult msgResult;

	uint64_t cid = 0;
	MailItem mItem;
	LIST_ITEM items;
	SItem itemInfo;

	string mtopical;
	string mcontent;

	retCode = webMail.ParseFromArray(package.dataAfterHead, package.sizeAfterHead);

	MMOLOG_PROCESS_ERROR(retCode);

	mtopical = webMail.topical();
	mcontent = webMail.content();

	//LogDebugFmtPrint("recv mail diamond:%d, gold :%d, arenaScore:%d", webMail.diamond(), webMail.gold(), webMail.arenascore());
	for (int i = 0; i < webMail.items_size(); i++)
	{
		mItem = webMail.items(i);
		itemInfo.nItemID = mItem.itemid();
		itemInfo.nNum = mItem.itemnum();
		items.push_back(itemInfo);
		LogDebugFmtPrint("recv mail item id:%d, num :%d", itemInfo.nItemID, itemInfo.nNum);
	}

	if (webMail.playerids_size() == 0)
		g_GetMailMgr()->SendZoneMail(mtopical, mcontent, items, webMail.gold(), webMail.bindgold(), webMail.diamond(), webMail.binddiamond(), webMail.arenascore(), MAIL_SOURCE_BACKEND);
	else
	{
		for (int i = 0; i < webMail.playerids_size(); i++)
		{
			cid = webMail.playerids(i);
			g_GetMailMgr()->SendSystemMail(cid, mtopical, mcontent, items, webMail.gold(), webMail.bindgold(), webMail.diamond(), webMail.binddiamond(), webMail.arenascore(), MAIL_SOURCE_BACKEND);
		}
	}

    result = true;
Exit0:
	msgResult.set_webid(webMail.webid());
	result ? msgResult.set_retcode(RET_SUCCESS) : msgResult.set_retcode(RET_FAIL);
	g_GetCenterService()->SendDataToWebOperate(WEB_SEND_MAIL_RSP, &msgResult);
	return result;
}

bool WebCenterMgr::SynWebOpInfo(RECV_PACKAGE& package)
{
    bool result = false;
    bool retCode = false;
	WebOpInfo webRsp;
	NotifyWebOpInfoToGate webOpInfo;

	retCode = webRsp.ParseFromArray(package.dataAfterHead, package.sizeAfterHead);
	MMOLOG_PROCESS_ERROR(retCode);
	webOpInfo.mutable_wpinfo()->CopyFrom(webRsp);
	retCode = g_GetCenterService()->SendDataToAllGate(EMODULE_ID_SERV_SYS, CENTER_NOTIFY_GATE_WEBOP_INFO, &webOpInfo);
	MMOLOG_PROCESS_ERROR(retCode);

	LogDebugPrint("recv synWebOpInfo trans to gate");
    result = true;
Exit0:
	return result;
}

bool WebCenterMgr::WebRumor(RECV_PACKAGE& package)
{
    bool result = false;
    bool retCode = false;
	WebRumorReq webRumor;
	ProtoBuf::GMResult msgResult;

	retCode = webRumor.ParseFromArray(package.dataAfterHead, package.sizeAfterHead);
	MMOLOG_PROCESS_ERROR(retCode);

	retCode = g_GetChatCenter()->ChatBackEndRumor(webRumor.content(), webRumor.loopcount());
	MMOLOG_PROCESS_ERROR(retCode);

    result = true;
Exit0:
	msgResult.set_webid(webRumor.webid());
	result ? msgResult.set_retcode(RET_SUCCESS) : msgResult.set_retcode(RET_FAIL);
	g_GetCenterService()->SendDataToWebOperate(WEB_SEND_RUMOR_RSP, &msgResult);

	return result;
}

bool WebCenterMgr::WebOpOpenReq(RECV_PACKAGE& package)
{
    bool result = false;
    bool retCode = false;

	WebPayOpenReq payopen;
	retCode = payopen.ParseFromArray(package.dataAfterHead, package.sizeAfterHead);
	MMOLOG_PROCESS_ERROR(retCode);

	for (int j = 0; j < payopen.payopens_size(); j++)
	{
		const ChannPayOpenInfo& po = payopen.payopens(j);
		g_GetPayCenterMgr()->SetPayOpen(po.channid(), po.open());
	}
	g_GetPayCenterMgr()->UpdateOtherPayToLogic();

    result = true;
Exit0:
	return result;
}

bool WebCenterMgr::WebOpSortReq(RECV_PACKAGE& package)
{
    bool result = false;
    bool retCode = false;

	WebSortIndexReq payindex;
	retCode = payindex.ParseFromArray(package.dataAfterHead, package.sizeAfterHead);
	MMOLOG_PROCESS_ERROR(retCode);
	for (int i = 0; i < payindex.indexs_size(); i++)
	{
		const ChannPayIndexInfo& pi = payindex.indexs(i);
		g_GetPayCenterMgr()->SetPayIndexs(pi.channid(), pi.indexs());
	}
	g_GetPayCenterMgr()->UpdateOtherPayToLogic();

    result = true;
Exit0:
	return result;
}

bool WebCenterMgr::WebCdKeyRspFromWeb(RECV_PACKAGE& package)
{
    bool result = false;
    bool retCode = false;
	CdKeyRsp rsp;
	retCode = rsp.ParseFromArray(package.dataAfterHead, package.sizeAfterHead);
	MMOLOG_PROCESS_ERROR(retCode);
	retCode = g_GetCenterService()->SendDataToClient(rsp.cid(), EMODULE_ID_RECV_ZONE_MSG, CLIENT_TO_CENTER_CDKEY_RSP, &rsp);
	MMOLOG_PROCESS_ERROR(retCode);

	LogDebugFmtPrint("WebCdKeyRspFromWeb, cid:%lu, retcode:%d", rsp.cid(), rsp.retcode());

Exit0:
	return result;
}

bool WebCenterMgr::WebCdKeyReqFromClient(RECV_PACKAGE& package)
{
    bool result = false;
    bool retCode = false;
	CdkeyReq req;
	UidGameInfo* pGameUidInfo = nullptr;

	retCode = req.ParseFromArray(package.dataAfterHead, package.sizeAfterHead);
	MMOLOG_PROCESS_ERROR(retCode);

	pGameUidInfo = g_GetCenterAccountMgr()->GetUidGameInfoByCid(req.cid());
	MMOLOG_PROCESS_ERROR(pGameUidInfo != nullptr);

	req.set_uid(pGameUidInfo->uid);
	req.set_zid(g_GetCenterService()->GetZoneId());

	retCode = g_GetCenterService()->SendDataToWebOperate(LOGIC_TO_WEBOP_CDKEY_REQ, &req);
	MMOLOG_PROCESS_ERROR(retCode);

	LogDebugPrint("WebCenterMgr::WebCdKeyReqFromClient");
    result = true;
Exit0:
	return result;
}